import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/user/login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/user/register.vue'),
    },
    {
      path: '/meun',
      name: 'meun',
      component: () => import('../views/menu.vue'),
      children: [
        { path: '/ai_zs', name: 'ai', component: () => import('../views/ai/ai.vue') },
        // 系统管理
        { path: '/sys_menu', name: 'menu', component: () => import('../views/sys/menu.vue') },
        { path: '/sys_role', name: 'role', component: () => import('../views/sys/role.vue') },
        { path: '/sys_user', name: 'user', component: () => import('../views/sys/user.vue') },

        // 财务管理
        { path: '/finance_income', name: 'finance-income', component: () => import('../views/finance/income.vue') },
        { path: '/finance_expense', name: 'finance-expense', component: () => import('../views/finance/expense.vue') },
        { path: '/finance_receive', name: 'finance-receive', component: () => import('../views/finance/receipt.vue') },
        { path: '/finance_pay', name: 'finance-pay', component: () => import('../views/finance/payment.vue') },
        { path: '/finance_column-bill', name: 'finance-column-bill', component: () => import('../views/finance/transfer.vue') },
        { path: '/finance_advance-receive', name: 'finance-advance-receive', component: () => import('../views/finance/advance.vue') },
        { path: '/finance_profit', name: 'finance-profit', component: () => import('../views/finance/report.vue') },

        // 采购管理
        { path: '/purchase_order', name: 'purchase-order', component: () => import('../views/purchase/purchase_order.vue') },
        { path: '/purchase_inbound', name: 'purchase-inbound', component: () => import('../views/purchase/purchase_inbound.vue') },
        { path: '/purchase_return', name: 'purchase-return', component: () => import('../views/purchase/purchase_return.vue') },

        // 销售管理
        { path: '/sales_order', name: 'sales-order', component: () => import('../views/sales/sales_order.vue') },
        { path: '/sales_outbound', name: 'sales-outbound', component: () => import('../views/sales/sales_outbound.vue') },
        { path: '/sales_return', name: 'sales-return', component: () => import('../views/sales/sales_return.vue') },

        // 仓库管理
        { path: '/warehouse_other-in', name: 'warehouse-other-in', component: () => import('../views/warehouse/warehouse_other-in.vue') },
        { path: '/warehouse_other-out', name: 'warehouse-other-out', component: () => import('../views/warehouse/warehouse_other-out.vue') },
        { path: '/warehouse_transfer', name: 'warehouse-transfer', component: () => import('../views/warehouse/warehouse_transfer.vue') },
        { path: '/warehouse_assembly', name: 'warehouse-assembly', component: () => import('../views/warehouse/warehouse_assembly.vue') },
        { path: '/warehouse_disassembly', name: 'warehouse-disassembly', component: () => import('../views/warehouse/warehouse_disassembly.vue') },

        // 零售管理
        { path: '/retail_outbound', name: 'retail-outbound', component: () => import('../views/retail/retail_outbound.vue') },
        { path: '/retail_return', name: 'retail-return', component: () => import('../views/retail/retail_return.vue') },

        // 盘点业务
        { path: '/check_input', name: 'check-input', component: () => import('../views/check/check_input.vue') },
        { path: '/check_recheck', name: 'check-recheck', component: () => import('../views/check/check_recheck.vue') },

        // 商品管理
        { path: '/product_category', name: 'product-category', component: () => import('../views/product/product_category.vue') },
        { path: '/product_info', name: 'product-info', component: () => import('../views/product/product_info.vue') },
        { path: '/product_unit', name: 'product-unit', component: () => import('../views/product/product_unit.vue') },
        { path: '/product_attributes', name: 'product-attributes', component: () => import('../views/product/product_attributes.vue') },

        // 生产管理
        { path: '/production_task', name: 'production-task', component: () => import('../views/production/production_task.vue') },
        { path: '/production_outsource', name: 'production-outsource', component: () => import('../views/production/production_outsource.vue') },
        { path: '/production_bom', name: 'production-bom', component: () => import('../views/production/production_bom.vue') },

        // CRM管理
        { path: '/crm_customer', name: 'crm-customer', component: () => import('../views/crm/crm_customer.vue') },
        { path: '/crm_member', name: 'crm-member', component: () => import('../views/crm/crm_member.vue') },
        { path: '/crm_reconciliation', name: 'crm-reconciliation', component: () => import('../views/crm/crm_reconciliation.vue') },
        { path: '/crm_followup', name: 'crm-followup', component: () => import('../views/crm/crm_followup.vue') },

        // 报表管理
        { path: '/report_product-stock', name: 'report-product-stock', component: () => import('../views/report/report_product-stock.vue') },
        { path: '/report_account-stat', name: 'report-account-stat', component: () => import('../views/report/report_account-stat.vue') },
        { path: '/report_retail-stat', name: 'report-retail-stat', component: () => import('../views/report/report_retail-stat.vue') },
        { path: '/report_purchase-stat', name: 'report-purchase-stat', component: () => import('../views/report/report_purchase-stat.vue') },
        { path: '/report_sales-stat', name: 'report-sales-stat', component: () => import('../views/report/report_sales-stat.vue') },
        { path: '/report_inout-stat', name: 'report-inout-stat', component: () => import('../views/report/report_inout-stat.vue') },
        { path: '/report_stock-warning', name: 'report-stock-warning', component: () => import('../views/report/report_stock-warning.vue') },
        { path: '/report_sales-ratio', name: 'report-sales-ratio', component: () => import('../views/report/report_sales-ratio.vue') },
        { path: '/report_sales-rank', name: 'report-sales-rank', component: () => import('../views/report/report_sales-rank.vue') },
        { path: '/report_sales-profit', name: 'report-sales-profit', component: () => import('../views/report/report_sales-profit.vue') },
        { path: '/report_negative-stock', name: 'report-negative-stock', component: () => import('../views/report/report_negative-stock.vue') },
        { path: '/report_multi-attr-stock', name: 'report-multi-attr-stock', component: () => import('../views/report/report_multi-attr-stock.vue') },
        { path: '/report_performance', name: 'report-performance', component: () => import('../views/report/report_performance.vue') },

        // 合同管理
        { path: '/contract_draft', name: 'contract-draft', component: () => import('../views/contract/contract_draft.vue') },
        { path: '/contract_approval', name: 'contract-approval', component: () => import('../views/contract/contract_approval.vue') },
        { path: '/contract_execution', name: 'contract-execution', component: () => import('../views/contract/contract_execution.vue') },
        { path: '/contract_test', name: 'contract-test', component: () => import('../views/contract/contract_test.vue') },

        // 设备管理
        { path: '/equipment_archive', name: 'equipment-archive', component: () => import('../views/equipment/equipment_archive.vue') },
        { path: '/equipment_maintenance', name: 'equipment-maintenance', component: () => import('../views/equipment/equipment_maintenance.vue') },
        { path: '/equipment_operation', name: 'equipment-operation', component: () => import('../views/equipment/equipment_operation.vue') },
      ]
    },
  ],
})

// 添加全局前置路由守卫
router.beforeEach((to, from, next) => {
  // 获取本地存储的用户名
  const username = localStorage.getItem("username");

  // 定义不需要登录的白名单路由
  const whiteList = ['login', 'register'];

  // 如果用户已登录，或者访问的是白名单中的路由，则放行
  if (username || whiteList.includes(to.name)) {
    next();
  } else {
    // 如果未登录且访问的不是白名单路由，重定向到登录页
    next('/');
  }
})

export default router

